package com.yunji.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunji.news.common.Result;
import com.yunji.news.entity.QualificationList;
import com.yunji.news.service.QualificationListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 入围名单控制器
 *
 * @author yunji
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/qualification")
@CrossOrigin
public class QualificationListController {

    @Autowired
    private QualificationListService qualificationService;

    /**
     * 分页查询入围名单（管理端）
     */
    @GetMapping("/admin")
    public Result<IPage<QualificationList>> getQualifications(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) String qualificationType,
            @RequestParam(required = false) String keyword) {
        
        IPage<QualificationList> result = qualificationService.getQualificationPage(
            page, size, schoolId, majorId, qualificationType, keyword);
        return Result.success(result);
    }

    /**
     * 获取公开的入围名单（前端展示）
     */
    @GetMapping("/public")
    public Result<List<QualificationList>> getPublicQualifications(
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) String qualificationType) {
        
        List<QualificationList> qualifications = qualificationService.getPublicQualificationList(
            schoolId, majorId, qualificationType);
        return Result.success(qualifications);
    }

    /**
     * 查询学生入围状态
     */
    @GetMapping("/student/{idCard}")
    public Result<QualificationList> getStudentQualification(
            @PathVariable String idCard,
            @RequestParam(required = false) Long schoolId) {
        
        QualificationList qualification = qualificationService.getStudentQualification(idCard, schoolId);
        if (qualification != null) {
            return Result.success(qualification);
        } else {
            return Result.error("未查询到入围信息");
        }
    }

    /**
     * 生成入围名单
     */
    @PostMapping("/generate")
    public Result<String> generateQualificationList(
            @RequestParam Long schoolId,
            @RequestParam Long majorId,
            @RequestParam String qualificationType,
            @RequestParam Integer count) {
        
        boolean success = qualificationService.generateQualificationList(
            schoolId, majorId, qualificationType, count);
        if (success) {
            return Result.success("入围名单生成成功");
        } else {
            return Result.error("入围名单生成失败");
        }
    }

    /**
     * 发布入围名单
     */
    @PutMapping("/publish")
    public Result<String> publishQualificationList(@RequestParam Long[] ids) {
        boolean success = qualificationService.publishQualificationList(ids);
        if (success) {
            return Result.success("入围名单发布成功");
        } else {
            return Result.error("入围名单发布失败");
        }
    }
}